有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将值从JTextField传递到方法的最佳方式?

目前正在使用BMR/TDEE计算器。该计算器包括用户的年龄、性别、体重和身高,并使用这些信息计算BMR和TDEE值。我已经为此创建了一个GUI,最后一步是进行实际计算。(还有一些小的改动要做,比如改变高度面板,但这与这个问题无关)

将实例变量传递给静态方法的最佳方式是什么?我的JTextField是类中的实例变量,我有两种方法分别计算BMR和TDEE(静态方法)

代码发布在下面

我需要使用的变量几乎都是textField变量,这些变量需要传递到calcBMR()和calcTDEE()。我最初的想法是让所有这些变量都是静态的,只需使用getText()并将这些值插入到我使用的公式中,该公式大致如下:

10倍体重(kg)+6.25倍身高(cm)-5倍年龄(y)+5

这种方法听起来可行吗?或者根据我的代码,有更好的方法吗?任何关于如何有效地做到这一点的建议都将不胜感激

public class BmrCalcv2 extends JFrame {

    // Frames and main panels
    static JFrame mainFrame;
    static JPanel mainPanel;
    static JPanel combinedGAHWpanel; // combines genderPanel, agePanel, heightPanel, weightPanel - this
                                     // is a BorderLayout, whereas
                                     // gender/agePanel are FlowLayouts.
    static JPanel weightHeightPanel; // Combines weight and height panel into out flowlayout panel, which is then combined into the above borderlayout panel

    // Image components
    static JPanel imgPanel;
    private JLabel imgLabel;
    private JLabel activityLevelHelp;

    // Menu-bar components
    static JMenuBar menuBar;
    static JMenu saveMenu, optionMenu, helpMenu;

    // Age components
    static JPanel agePanel;
    private JLabel ageLabel;
    private JLabel yearsLabel;
    private JTextField ageTextField;

    // Gender components
    static JPanel genderPanel;
    private JLabel genderLabel;
    private JRadioButton genderMale;
    private JRadioButton genderFemale;


    // Height components
    static JPanel heightPanel;
    private JLabel heightLabel;
    private JTextField heightCMField;
    private JLabel heightFTLabel;
    private JLabel heightINCHLabel;
    private JTextField heightFTField;
    private JTextField heightINCHField;
    private JToggleButton cmButton;
    private JToggleButton feetButton;


    // Weight components
    static JPanel weightPanel;
    private JLabel weightLabel;
    private JTextField weightField;
    private JToggleButton kgButton;
    private JToggleButton lbButton;


    // TDEE and BMR Components
    static JPanel tdeePanel;
    static JPanel tdeeBMRPanel;
    static JPanel activityLevelPanel;
    static JPanel bmrTDEEValuesPanel;
    static JPanel bmrValuePanel;
    static JPanel tdeeValuePanel;
    private JLabel tdeeQuestionLabel;
    private JLabel activityLevelLabel;
    private JComboBox activityLevelBox;
    private JRadioButton tdeeYes;
    private JRadioButton tdeeNo;
    private JLabel bmrLabel;
    private JLabel tdeeLabel;
    private JButton calculate;




    // Default values for gender/weight/height and other variables

    String[] activityLevels = {"Sedentary", "Lightly Active", "Moderately Active", "Very Active", "Extra Active"};
    String genderSelection = "M";   
    String weightSelection = "kg";
    String heightSelection = "cm";
    String tdeeSelection = "no";

    String ageValue;
    String weightValue;
    String heightValue;
    static String bmrValue = "N/A";
    static String tdeeValue = "N/A";



    public BmrCalcv2(String title) {

        // Main JFrame
        setTitle("BMR/TDEE Calculator");
        mainPanel = new JPanel();

        // All JPanel declarations
        menuBar = new JMenuBar();
        imgPanel = new JPanel();
        agePanel = new JPanel();
        genderPanel = new JPanel();
        heightPanel = new JPanel();
        weightPanel = new JPanel();
        weightHeightPanel = new JPanel(new BorderLayout());
        combinedGAHWpanel = new JPanel(new BorderLayout()); // Create a new panel used to combine
                                                            // genderPanel, agePanel, weightPanel, heightPanel below
        tdeeBMRPanel = new JPanel(new BorderLayout());
        tdeePanel = new JPanel();
        activityLevelPanel = new JPanel();
        bmrTDEEValuesPanel = new JPanel(new BorderLayout());
        bmrValuePanel = new JPanel();
        tdeeValuePanel = new JPanel();



        // Image panel declaration
        imgLabel = new JLabel(new ImageIcon("filesrc//mainlogo.png"));
        activityLevelHelp = new JLabel(new ImageIcon("filesrc//question-mark.png"));
        imgPanel.add(imgLabel);

        // JPanel layout managers
        agePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        genderPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));


        // Menu JComponents
        saveMenu = new JMenu("Save");
        optionMenu = new JMenu("Options");
        helpMenu = new JMenu("Help");
        menuBar.add(saveMenu);
        menuBar.add(optionMenu);
        menuBar.add(helpMenu);

        // Age JComponents
        ageLabel = new JLabel("Age:");
        yearsLabel = new JLabel("<html><i>years</i><html>");
        ageTextField = new JTextField(5);
        agePanel.add(ageLabel);
        agePanel.add(ageTextField);
        agePanel.add(yearsLabel);

        // Gender JComponents
        genderLabel = new JLabel("Gender:");
        genderMale = new JRadioButton("Male", true);
        genderFemale = new JRadioButton("Female");



        genderPanel.add(genderLabel);
        genderPanel.add(genderMale);
        genderPanel.add(genderFemale);


        ButtonGroup genderGroup = new ButtonGroup(); // groups male and female radio buttons together so that only one can be selected
        genderGroup.add(genderMale);
        genderGroup.add(genderFemale);


        genderMale.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
              String genderSelection = "M";   
            } 
        });

        genderFemale.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
              String genderSelection = "F";
            } 
        });

        // Height JComponents
        heightLabel = new JLabel("Height:");
        heightCMField = new JTextField(4);
        heightFTField = new JTextField(3);
        heightFTLabel = new JLabel("ft");
        heightINCHLabel = new JLabel("inch");
        heightINCHField = new JTextField(3);
        cmButton = new JToggleButton("cm", true);
        feetButton = new JToggleButton("feet");
        heightPanel.add(heightLabel);


        ButtonGroup heightGroup = new ButtonGroup();
        heightGroup.add(cmButton);
        heightGroup.add(feetButton);


        heightPanel.add(heightCMField);
        heightPanel.add(heightFTField);
        heightPanel.add(heightFTLabel);
        heightPanel.add(heightINCHField);
        heightPanel.add(heightINCHLabel);
        heightPanel.add(cmButton);
        heightPanel.add(feetButton);
        heightFTField.setVisible(false);
        heightFTLabel.setVisible(false);
        heightINCHField.setVisible(false);
        heightINCHLabel.setVisible(false);


        cmButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                  heightSelection = "cm";
                  heightINCHField.setVisible(false);
                  heightFTField.setVisible(false);
                  heightFTLabel.setVisible(false);
                  heightINCHLabel.setVisible(false);
                  heightCMField.setVisible(true);

                  weightPanel.revalidate();
                  weightPanel.repaint();
                }
            });

        feetButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                  heightSelection = "feet";
                  heightINCHField.setVisible(true);
                  heightFTField.setVisible(true);
                  heightFTLabel.setVisible(true);
                  heightINCHLabel.setVisible(true);
                  heightCMField.setVisible(false);

                  weightPanel.revalidate();
                  weightPanel.repaint(); 
                }
            });


        // Weight JComponents
        weightLabel = new JLabel("Weight:");
        weightField = new JTextField(4);
        kgButton = new JToggleButton("kg", true);
        lbButton = new JToggleButton("lbs");



        weightPanel.add(weightLabel);
        weightPanel.add(weightField);
        weightPanel.add(kgButton);
        weightPanel.add(lbButton);



        ButtonGroup weightGroup = new ButtonGroup();
        weightGroup.add(kgButton);
        weightGroup.add(lbButton);




        kgButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                weightSelection = "kg"; 
            }
        });

        lbButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                weightSelection = "lb"; 
            }
        });


        // tdee JComponents
        tdeeQuestionLabel = new JLabel("Calculate TDEE Also?");
        tdeeYes = new JRadioButton("Yes");
        tdeeNo = new JRadioButton("No", true);

        ButtonGroup tdeeButton = new ButtonGroup();
        tdeeButton.add(tdeeYes);
        tdeeButton.add(tdeeNo);

        tdeePanel.add(tdeeQuestionLabel);
        tdeePanel.add(tdeeYes);
        tdeePanel.add(tdeeNo);


        // activitylevel JComponents

        activityLevelLabel = new JLabel("Activity Level: ");
        activityLevelBox = new JComboBox(activityLevels);
        activityLevelBox.setSelectedIndex(0);

        activityLevelPanel.add(activityLevelLabel);
        activityLevelPanel.add(activityLevelBox);
        activityLevelPanel.add(activityLevelHelp);
        activityLevelBox.setEnabled(false);

        activityLevelHelp
                .setToolTipText("<html><b>Sedentary:</b> little or no exercise, deskjob<<br /><b>Lightly Active:</b> little exercise/sports 1-3 days/week<br /><b>Moderately active:</b> moderate exercise/sports 3-5 days/week<br /><b>Very active:</b> hard exercise or sports 6-7 days/week<br /><b>Extra active:</b> hard daily exercise or sports & physical labor job </html>");

        tdeeYes.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                  tdeeSelection = "yes";
                  activityLevelBox.setEnabled(true);
                }
            });

        tdeeNo.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                  tdeeSelection = "no";
                  activityLevelBox.setEnabled(false);
                }
            });



        // tdee and BMR value components

        bmrValue = calcBmr();

        bmrLabel = new JLabel("<html><br /><br /><font size=4>You have a <i><font color=red>BMR</font></i> of: " + bmrValue + "<font></html>");
        tdeeLabel = new JLabel("<html><br /><font size=4>You have a <i><font color=red>TDEE</font></i> of: " + tdeeValue + "<font></html>");
        calculate = new JButton("Calculate");

        bmrTDEEValuesPanel.add(calculate, BorderLayout.NORTH);
        bmrTDEEValuesPanel.add(bmrLabel, BorderLayout.CENTER);
        bmrTDEEValuesPanel.add(tdeeLabel, BorderLayout.SOUTH);

        // Debugging panels for buttons (remove when complete)
        calculate.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                  System.out.println("Male:" + genderMale.isSelected());
                  System.out.println("Female:" + genderFemale.isSelected() + "\n");
                  System.out.println("Kg:" + kgButton.isSelected());
                  System.out.println("LB:" + lbButton.isSelected() + "\n");
                  System.out.println("CM:" + cmButton.isSelected());
                  System.out.println("Feet:" + feetButton.isSelected() + "\n");
                  System.out.println("TDEE Yes:" + tdeeYes.isSelected());
                  System.out.println("TDEE No:" + tdeeNo.isSelected());
                  System.out.println("-------------------------------------");

                }
            });




        // Adding sub JPanels to main JPanel
        mainPanel.add(imgPanel);

        combinedGAHWpanel.add(agePanel, BorderLayout.NORTH); // Combine genderPanel and agePanel (which are both flowLayouts) into a
                                                             // single BorderLayout panel where agePanel is given the Northern spot and
                                                             // genderPanel is given the center spot in the panel
        weightHeightPanel.add(weightPanel, BorderLayout.NORTH); // Nested borderlayouts, the weightHeightPanel is another borderLayout which is nested
                                                                 // into the southern position of the combinedGAHW border layout.
        weightHeightPanel.add(heightPanel, BorderLayout.CENTER);
        weightHeightPanel.add(tdeeBMRPanel, BorderLayout.SOUTH);




        combinedGAHWpanel.add(genderPanel, BorderLayout.CENTER);
        combinedGAHWpanel.add(weightHeightPanel, BorderLayout.SOUTH);


        mainPanel.add(combinedGAHWpanel);


        // adding to tdeeBMRPanel
        tdeeBMRPanel.add(tdeePanel, BorderLayout.NORTH);
        tdeeBMRPanel.add(activityLevelPanel, BorderLayout.CENTER);


        tdeeBMRPanel.add(bmrTDEEValuesPanel, BorderLayout.SOUTH);


        // Adding main JPanel and menubar to JFrame
        setJMenuBar(menuBar);
        add(mainPanel);
    }



    public static String calcBmr() {


        return bmrValue;
    }


    public static String calcTDEE() {

        return tdeeValue;
    }


    public static void main(String[] args) {

        BmrCalcv2 gui = new BmrCalcv2("BMR/TDEE Calculator");

        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.setSize(330, 500);
        gui.setResizable(false);

    }
}

谢谢


共 (0) 个答案